import { UICallbacks, ViewParams } from "./Defines";
import { UIConfig } from "./LayerManager";
import { LayerPopUp } from "./LayerPopUp";

type DialogParam = {
    config:UIConfig,
    params?:any,
    callbacks?:UICallbacks
}

export class LayerDialog extends LayerPopUp{

    private params:Array<DialogParam> = []
    //添加DiaLog
    public add(config:UIConfig,params?:any,callbacks?:UICallbacks){
        //如果当前界面有多个
        if (this.ui_nodes.size>0) {
            this.params.push({
                config:config,
                params:params,
                callbacks:callbacks
            })
            return;
        }
        this.black.enabled = true;
        this.show(config,params,callbacks)
    }

    //显示Dialog
    private show(config:UIConfig,params?:any,callbacks?:UICallbacks):void{
        let vp = this.ui_nodes.get(config.prefab);
        if (vp==null) {
            vp = new ViewParams();
            vp.valid = true;
            vp.config = config;
        }
        vp.params = params || {};
        vp.callback = callbacks ?? {}

        this.ui_nodes.set(config.prefab,vp);

        this.load(vp,config.bundle);
        
    }
    protected onCloseWindow(vp: ViewParams): void {
        super.onCloseWindow(vp);
        setTimeout(this.next.bind(this),0)
    }

    protected setBlackDisable(): void {
        if (this.params.length == 0) {
            this.black.enabled = false;
            this.closeVacancyRemove();
            this.closeMask();
        }
    }

    private next():void{
        if (this.params.length>0) {
            let param  = this.params.shift()!;
            this.show(param.config,param?.params,param?.callbacks);
        }
    }

}